var _CombineScene = require('component/combinescene.js');
var _global = require('global/global.js');
var _geometry = require('core/glm.js');
var _vec3 = _geometry.vec3f;

var TWEEN = require('third_party/tweenjs/tween.js');
var GC = require('nativeclasses').gcobserver;

// ==========================
// SlideAnima
// ==========================
function SlideAnima(outer, target, listener) {
    this.mOuter = outer;
    this.mTarget = target;
    this.mListener = listener;

    this.x = 0;
    this.y = 0;

    this.mTox = 0;
    this.mToy = 0;
    this.mTime = 0.7;
    this.mEasing = TWEEN.Easing.Cubic.InOut;
}
SlideAnima.prototype.setTime = function (t) {
    this.mTime = t;
    return this;
};
SlideAnima.prototype.setEasing = function (e) {
    this.mEasing = e;
    return this;
};
SlideAnima.prototype.setDest = function (x, y) {
    this.mTox = x;
    this.mToy = y;
    return this;
};
SlideAnima.prototype.setStart = function (x, y) {
    this.x = x;
    this.y = y;
    return this;
};
// ---------------- tween bindings
SlideAnima.prototype.onComplete = function () {
    this.mOuter = null;
    this.mTarget = null;
    this.mListener.trigger();
    this.mListener.release();
    this.mListener = null;
};
SlideAnima.prototype.onUpdate = function () {
    var position = this.mTarget.__position;
    position.set(this.x, this.y);
    this.mOuter.getSize().mul(position, this.mTarget.getPosition());
    this.mTarget.touchMatrix();
};
/**
 * @param x
 * @param y
 * @param tox
 * @param toy
 * @returns {*}
 */
SlideAnima.prototype.start = function () {
    return new TWEEN.Tween(this)
        .to({x: this.mTox, y: this.mToy}, this.mTime)
        .easing(this.mEasing)
        .onUpdate(this.onUpdate)
        .onComplete(this.onComplete)
        .start();
};

// ==========================
// Switch
// ==========================
var Switch = _CombineScene.createCombine(function () {
    var w = _global.screen.width();
    var h = _global.screen.height();
    this.setSize(w, h);
    this.setId('switch');

    this.__position = new _vec3();
});
Switch.prototype.createAnima = function (target, listener) {
    return new SlideAnima(this, target, listener);
};
/**
 * add scene with a fix position
 * @param scene
 * @param x
 * @param y
 */
Switch.prototype.addScene = function (scene, x, y) {
    this.addChild(scene);
    this.getSize().mul(scene.__position = new _vec3(x, y), scene.getPosition());
    scene.touchMatrix();
};
var count = 1;
/**
 * @param scene
 * @param x
 * @param y
 * @param event
 * @returns {*}
 */
Switch.prototype.tweenScene = function (scene, x, y, event) {
    var out = this;
    var position = scene.__position = new _vec3(x, y);

    // update child position immediately
    out.getSize().mul(position, scene.getPosition());
    scene.touchMatrix();

    // create anima
    var aniam = new TWEEN.Tween({x: x, y: y}).
        onUpdate(function () {
            position.set(this.x, this.y);
            out.getSize().mul(position, scene.getPosition());
            scene.touchMatrix();
        }).
        onComplete(function () {
            out.fire(event, out);
            out.off(event);
        });
    scene.__gc1 = new GC('Switch.prototype.tweenScene.scene==' + count);
    aniam.__gc = new GC('Switch.prototype.tweenScene' + count);
    return aniam;
};
/**
 * create tween anima on wrap scene
 * @param scene
 * @param x init xy
 * @param y
 * @returns {*}
 */
Switch.prototype.tween = function (x, y, event) {
    this.__position.set(x, y);
    var out = this;
    var position = out.__position;

    // update current scene's positioin
    out.getSize().mul(position, out.getPosition());
    out.touchMatrix();

    // create anima
    var aniam = new TWEEN.Tween({x: x, y: y}).
        onUpdate(function () {
            position.set(this.x, this.y);
            out.getSize().mul(position, out.getPosition());
            out.touchMatrix();
        }).
        onComplete(function () {
            out.fire(event, out);
            out.off(event);
        });
    aniam.__gc = new GC('Switch.prototype.tween');
    return aniam;
};
Switch.prototype.onSizeChange = function (w, h) {
    this.superOnSizeChange(w, h);

    // update current position
    var size = this.getSize();
    size.mul(this.__position, this.getPosition());
    this.touchMatrix();

    // update current position
    for (var i = 0, children = this.mChildren, l = children.length; i < l; i++) {
        var child = children[i];
        size.mul(child.__position, child.getPosition());
        child.touchMatrix();
    }
};
Switch.prototype.onUnregister = function (global) {
    _CombineScene.prototype.onUnregister.call(this, global);
    this.mEvents.release();
    for (var i = 0, children = this.mChildren, l = children.length; i < l; i++) {
        delete children[i].__position;
    }
    delete this.__position;
};

module.exports = Switch;
